//
//  KFDMarketNewsAliveTableViewCell.m
//  
//
//  Created by JIRENTIANXIANG on 6/12/15.
//
//

#import "KFDMarketNewsAliveTableViewCell.h"
#import "NSDate+format.h"
#import "ui_config.h"
#import "JRTXLinearLayout.h"
#import <SDWebImage/SDWebImageManager.h>
#import <SDWebImage/UIImageView+WebCache.h>
@interface KFDMarketNewsAliveTableViewCell ()

@property (assign,nonatomic) CGFloat height;

@property (weak, nonatomic) IBOutlet UIImageView *timeDot;
@property (strong,nonatomic) UILabel* dateLabel;
@property (strong,nonatomic) UILabel* timeLabel;
@property (strong,nonatomic) JRTXLinearLayout* contentLayout;

@property (nonatomic,strong) UIImageView *tipsView;
@property (nonatomic,strong) UILabel *tipsLabel;

@end

@implementation KFDMarketNewsAliveTableViewCell

- (void)awakeFromNib {
    // Initialization code
    [self initDateLabel];
    [self initTimeLabel];
    [self initContentLayout];
    [self initTipsView];
}

-(void)initDateLabel
{
    _dateLabel=[[UILabel alloc] initWithFrame:CGRectMake(24, 26, 30, 10)];
    _dateLabel.textColor=KFDColorRGB(0x8f, 0x90, 0x8d);
    _dateLabel.font=[UIFont systemFontOfSize:10];
    [self addSubview:_dateLabel];
}

-(void)initTimeLabel
{
    CGRect frame=CGRectMake(11, 11, 40, 13);
//    UIImageView* timeLabelBg=[[UIImageView alloc] initWithFrame:frame];
//    timeLabelBg.image=[UIImage imageNamed:@"time-box"];
//    [self addSubview:timeLabelBg];
    
    _timeLabel=[[UILabel alloc] initWithFrame:frame];
    _timeLabel.textColor=[UIColor whiteColor];
    _timeLabel.font=[UIFont systemFontOfSize:8];
    _timeLabel.textAlignment=NSTextAlignmentCenter;
    _timeLabel.backgroundColor=[UIColor blackColor];
    _timeLabel.layer.cornerRadius=3;
    _timeLabel.layer.masksToBounds=YES;
    [self addSubview:_timeLabel];
}

-(void)initContentLayout
{
    self.contentLayout=[[JRTXLinearLayout alloc] initWithFrame:CGRectMake(78, 10, SCREEN_WIDTH-78-10, 0)];
    [self addSubview:self.contentLayout.view];
}
- (void)initTipsView {
    self.tipsView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self addSubview:self.tipsView];
    
    self.tipsView.hidden = YES;
    
    self.tipsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.tipsLabel.backgroundColor = [UIColor clearColor];
    self.tipsLabel.font = [UIFont systemFontOfSize:11];
    self.tipsLabel.textAlignment = NSTextAlignmentCenter;
    [self.tipsView addSubview:self.tipsLabel];
    
    self.tipsLabel.hidden = YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)drawRect:(CGRect)rect
{
    CGContextRef context=UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, KFD_WHITE.CGColor);
    CGContextFillRect(context, rect);
    
    //timeline
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 1);
    CGRect frame=self.timeDot.frame;
    CGFloat x=frame.origin.x+frame.size.width*0.5;
    CGFloat y0=0;
    CGFloat y1=frame.origin.y;
    CGFloat y2=y1+frame.size.height;
    CGFloat y3=self.frame.size.height;
    CGPoint timeLinePoints[4]={
        CGPointMake(x, y0),
        CGPointMake(x, y1),
        CGPointMake(x, y2),
        CGPointMake(x, y3)
    };
    CGContextStrokeLineSegments(context, timeLinePoints, 4);
}

-(void)setNewsPiece:(KFDLiveNewsPiece *)newsPiece
{
    _newsPiece=newsPiece;
    
    if ([self isNotEmptyString:_newsPiece.datainfo]) {
        NSLog(@"%@",_newsPiece.datainfo);
    }
    
    [self setTime:newsPiece.dateline.integerValue];
    [self setContent:newsPiece.content];
    [self setPictureUrl:newsPiece.picture];
    [self layoutTipsView];
}

-(void)setTime:(NSInteger)time
{
    NSDate* date=[NSDate dateWithTimeIntervalSince1970:time];
    self.dateLabel.text=date.month_day;
    self.timeLabel.text=date.hour_minute_second;
}
-(void)setContent:(NSString*)content
{
    UILabel* contentLabel=[[UILabel alloc] init];
    contentLabel.text=content;
    contentLabel.font=self.newsPiece.isbold?[UIFont boldSystemFontOfSize:12]:[UIFont systemFontOfSize:12];
    contentLabel.textColor=self.newsPiece.color?:KFDColorRGB(0x6d, 0x6d, 0x6d);
    [self.contentLayout appendLabel:contentLabel widthLimit:SCREEN_WIDTH-75-20 alignment:JRTXLinearLayoutAlignmentLeft];
    [self resetHeight];
}
- (BOOL)isNotEmptyString:(NSString *)str {
    if (!str) {
        return NO;
    }
    
    if (![str isKindOfClass:[NSString class]]) {
        return NO;
    }
    
    if ([str isEqualToString:@""]) {
        return NO;
    }
    
    return YES;
}
- (unsigned)hexFromString:(NSString*)str {
    unsigned hex;
    NSScanner* scanner=[NSScanner scannerWithString:str];
    [scanner setScanLocation:0];
    [scanner scanHexInt:&hex];
    return hex;
}
- (void)layoutTipsView {
    if ([self isNotEmptyString:_newsPiece.datainfo]) {
        self.tipsView.hidden = NO;
        self.tipsView.frame = CGRectMake(80, CGRectGetMaxY(self.contentLayout.view.frame) + 2, 52, 12);
        
        NSString *string = _newsPiece.datapic;
        if ([self isNotEmptyString:_newsPiece.datapic]) {
            [self.tipsView sd_setImageWithURL:[NSURL URLWithString:string]];
        }
        
        NSString *text = _newsPiece.datainfo;
        if ([self isNotEmptyString:text]) {
            self.tipsLabel.hidden = NO;
            self.tipsLabel.frame = self.tipsView.bounds;
            
            self.tipsLabel.text = text;
            
            NSString *color = _newsPiece.datacolor;
            if (color && color.length==7) {
                NSString* r = [color substringWithRange:NSMakeRange(1, 2)];
                NSString* g = [color substringWithRange:NSMakeRange(3, 2)];
                NSString* b = [color substringWithRange:NSMakeRange(5, 2)];
                self.tipsLabel.textColor = KFDColorRGB([self hexFromString:r], [self hexFromString:g], [self hexFromString:b]);
            }
        }
    }
    else {
        self.tipsView.hidden = YES;
        self.tipsView.image = nil;
        
        self.tipsLabel.hidden = YES;
        self.tipsLabel.text = nil;
    }
}
-(void)setPictureUrl:(NSString*)url
{
    if (url&&url.length>0) {
        CGFloat pictureWidth=SCREEN_WIDTH-75-20;
        UIImageView* picture=[[UIImageView alloc] init];
        SDWebImageManager* webImageManager=[SDWebImageManager sharedManager];
        UIImage* image=[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:[webImageManager cacheKeyForURL:[NSURL URLWithString:url]]];
        if (image) {
            picture.image=image;
            CGSize imageSize=image.size;
            CGFloat calculatedPictureHeight=pictureWidth*imageSize.height/imageSize.width;
            picture.frame=CGRectMake(0, 0, pictureWidth, calculatedPictureHeight);
            [self.contentLayout appendBlankLineWithHeight:10];
            [self.contentLayout appendView:picture alignment:JRTXLinearLayoutAlignmentLeft];
            [self.contentLayout appendBlankLineWithHeight:10];
            [self resetHeight];
            self.marketInfoContainer.newsLiveTableViewCellHeights[[NSString stringWithFormat:@"%ld",(long)self.indexPath.row]]=@(self.height);
        }else{
            
        }
    }
}

-(void)resetHeight
{
    self.height=20+self.contentLayout.view.bounds.size.height;
    self.timeLabel.frame=CGRectMake(11, self.height/2-11, 40, 13);
    self.dateLabel.frame=CGRectMake(20, self.height/2+3, 40, 10);
    
    if ([self isNotEmptyString:_newsPiece.datainfo]) {
        self.height += 16;
    }
}

@end
